package br.com.sgp.util.handling;

import org.apache.wicket.ajax.attributes.AjaxCallListener;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.model.IModel;

public abstract class ErrorHandlingAjaxConfirmLink extends ErrorHandlingAjaxLink {
    private static final long serialVersionUID = -2962432791206459217L;

    private final IModel<String> confirmMessage;

    public ErrorHandlingAjaxConfirmLink(final String id, final IModel<String> confirmMessage) {
        super(id);
        this.confirmMessage = confirmMessage;
    }

    @Override
    protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
        super.updateAjaxAttributes(attributes);
        final AjaxCallListener ajaxCallListener = new AjaxCallListener();
        ajaxCallListener.onPrecondition("return confirm('" + confirmMessage.getObject() + "');");
        attributes.getAjaxCallListeners().add(ajaxCallListener);
    }

}
